package com.yq.light.blog.code.controller;

import com.yq.light.blog.code.service.BlogService;
import com.yq.light.commons.code.entity.Bolog;
import com.yq.light.commons.code.entity.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/blog")
@Slf4j
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/blogList")
    @ResponseBody
    public CommonResult blogList(@RequestParam("ownerAccount") String ownerAccount){
        log.info("blogList require param ownerAccount--"+ownerAccount);
        try{
            List<Bolog> list = blogService.blogList(ownerAccount);
            if(list != null){
                return new CommonResult(200,"请求成功",list);
            }
        }catch (Exception e){
            log.error("BlogController--"+"blogList--"+e.toString());
        }
        return new CommonResult(200,"请求失败","null,无结果！");
    }

    /**
     *
     * @param ownerNickname
     * @param ownerAccount
     * @param title
     * @param contentLimit
     * @param blogCover
     * @return
     */
    @RequestMapping("/newBlog")
    @ResponseBody
    public CommonResult newBlog(@RequestParam("ownerNickname") String ownerNickname,
                                @RequestParam("ownerAccount") String ownerAccount,
                                @RequestParam("title") String title,
                                @RequestParam("contentLimit") String contentLimit,
                                @RequestParam("bologCover") List<MultipartFile> blogCover){

        Bolog bologInfo = new Bolog();
        if(blogCover.isEmpty()){
            return new CommonResult(200,"封面上传失败！",0);
        }else{
            Date da = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(MultipartFile file : blogCover){
                String path = "C:\\WorkProject\\static_resources\\blog\\blog_cover";
                File filePath =new File(path);
                if(!filePath.exists()){
                    filePath.mkdirs();
                }
                String baseUrl = "http://localhost:8041/uploadBlogCover/";
                String newFileName = baseUrl+ownerAccount+sdf+".jpg";
                log.info("博客封面图存放在:" + path +"  "+ newFileName);
                try{
                    file.transferTo(new File(path+"\\"+newFileName));
                    bologInfo.setBlogCover(newFileName);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            bologInfo.setOwnerNickname(ownerNickname);
            bologInfo.setOwnerAccount(ownerAccount);
            bologInfo.setTitle(title);
            bologInfo.setContentLimit(contentLimit);



            Timestamp date = Timestamp.valueOf(sdf.format(da));
            bologInfo.setCreateTime(date);

        }

        try{
            int result = blogService.newBlog(bologInfo);
            if(result > 0){
                return new CommonResult(200,"请求成功",result);
            }
        }catch (Exception e){
            log.error("BlogController--"+"blogList--"+e.toString());
        }
        return new CommonResult(200,"请求失败","null,无结果！");
    }
}
